﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

using BabyFood.Entities;
using BabyFood.Data;

public partial class Admin_SachHay_UploadImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UploadImage.DialogFilter = "Image Files(*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.GIF";
            if (Request.QueryString["sFileName"].ToString().Length > 0)
            {
                H_FileChonLai.Value = Request.QueryString["sFileName"].ToString();
            }
        }
    }

    protected void cmdUpload_Click(object sender, EventArgs e)
    {
    }

    protected void UploadImage_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
    {
        // Anh goc
        string imgNewsPath = Server.MapPath(Common.sPathUpSachHay);

        // Anh thumbail
        string imgNewsThumbail = Server.MapPath(Common.sPathUpSachHayThumbail);

        // Kiem tra xem co ton tai thu muc do chua
        if (!Directory.Exists(imgNewsPath)) Directory.CreateDirectory(imgNewsPath);
        if (!Directory.Exists(imgNewsThumbail)) Directory.CreateDirectory(imgNewsThumbail);

        System.Drawing.Image originalImg = System.Drawing.Image.FromStream(args.OpenStream());

        Bitmap imgNewThumb = ResizeImage(originalImg, 80);
        Bitmap imgNewTemp = GetBitMap(imgNewThumb, false);
        string sFileName = Path.GetFileNameWithoutExtension(args.FileName) + DateTime.Now.ToString("ddMMyyhhmmss") + Path.GetExtension(args.FileName);
        imgNewTemp.Save(imgNewsThumbail + "\\" + sFileName); // Luu anh da crop
        originalImg.Save(imgNewsPath + "\\" + sFileName); // Luu anh goc
        imgNewThumb.Dispose();
        imgNewTemp.Dispose();
        string strImgInsert = "../../Upload/SachHay/" + sFileName;
        string strInsertImg = "<script>";
        strInsertImg = strInsertImg + "parent.parent.Insert_Image('" + strImgInsert + "','" + sFileName + "');";
        strInsertImg = strInsertImg + "parent.parent.GB_hide();";
        strInsertImg = strInsertImg + "</script>";
        Page.RegisterClientScriptBlock("strInsertImg", strInsertImg);
    }

    protected void UploadImage_AttachmentRemoveClicked(object sender, CuteWebUI.AttachmentItemEventArgs args)
    {
        string imgNewsPath = Server.MapPath(Common.sPathUpSachHay);
        string sFileName = Path.GetFileNameWithoutExtension(args.Item.FileName) + DateTime.Now.ToString("ddMMyyhhmmss") + Path.GetExtension(args.Item.FileName);
        if (File.Exists(imgNewsPath + "\\" + sFileName))
        {
            File.Delete(imgNewsPath + "\\" + sFileName);
        }
    }

    protected Bitmap GetBitMap(Bitmap bitmapTemp, bool insertText)
    {
        Bitmap bmpNew = new Bitmap(bitmapTemp.Width, bitmapTemp.Height);
        Graphics oGraphic = Graphics.FromImage(bmpNew);
        oGraphic.DrawImage(bitmapTemp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmpNew.Width, bmpNew.Height, GraphicsUnit.Pixel);
        bitmapTemp.Dispose();
        bitmapTemp = bmpNew;
        if (insertText)
        {
            SolidBrush oBrush = new SolidBrush(Color.Blue);
            Font oFont = new Font("Arial", 9, FontStyle.Bold);
            oGraphic.DrawString("GiaiTri", oFont, oBrush, 10, 10);
            oFont.Dispose();
            oBrush.Dispose();
        }
        oGraphic.Dispose();
        return bitmapTemp;
    }

    protected Bitmap ResizeImage(System.Drawing.Image image, int maxHeight)
    {
        var ratio = (double)maxHeight / image.Height;

        var newWidth = (int)(image.Width * ratio);
        var newHeight = (int)(image.Height * ratio);

        var newImage = new Bitmap(newWidth, newHeight);
        using (var g = Graphics.FromImage(newImage))
        {
            g.DrawImage(image, 0, 0, newWidth, newHeight);
        }
        return newImage;
    }

    public enum AnchorPosition
    {
        Top,
        Center,
        Bottom,
        Left,
        Right
    }
}
